<?php

//商户商品栏目
if (!defined('IN_T')) {
    die('hacking attempt');
}
$id = get_company_id();
//查看栏目列表
if ($act == "index") {
    $size = 15;
    $page = intval($_REQUEST['page']);
    $page = $page<1 ? 1 : $page;
    $res = productCatList($id,$page,$size);
    $tp->assign("list",$res);

    $pages = Common::set_page($size,$page,$res['count']);
    foreach ($pages as $key => $value) {
        $pages[$key]['url'] = "/".COMPANY_PATH."/?m=new_cat&page=".$value['num'].$res['spm'];
    }
    $tp->assign("page",$page);
    $tp->assign("size",$size);
    $tp->assign("pages",$pages);
} //编辑商品分类
else if ($act == "detail") {
    $company_new_cat_id = intval($_REQUEST['company_new_cat_id']);
    //验证$company_new_cat_id是否合法
    $company_new_cat = $Db->query("SELECT * FROM " . $Base->table('company_new_cat') . " WHERE id = $company_new_cat_id", "Row");
    if (empty($company_new_cat)) {
        $company_new_cat_id = 0;
    }
    //前台显示
    if (empty($_POST)) {
        //查询管理的分类
        $sql = "select * from ".$Base->table("company_admin_new_cat");
        $admin_list = $Db->query($sql);
        $tp->assign('company_types',$admin_list);

        $tp->assign('company_new_cat', $company_new_cat);

    } //后台数据处理
    else {
        $res['status'] = 0;
        $data = array(
            'pid' => Common::sfilter($_POST['pid']),
            'href' => Common::sfilter($_POST['href']),
            'sort' => intval($_POST['sort']),
            'c_id' => $id,
        );

        if (empty($data['pid'])) {
            $res['msg'] = '分类名称不能为空';
        }
        else {
            if ($company_new_cat_id) {
                $Db->update($Base->table('company_new_cat'), $data, array('id' => $company_new_cat_id));
            } else {
                $company_new_cat_id = $Db->insert($Base->table('company_new_cat'), $data);
            }
            $res = array('status' => 1, 'msg' => '提交成功', 'href' => '/' . COMPANY_PATH . '/?m=new_cat');
        }
        echo $Json->encode($res);
        exit;
    }
} //删除栏目分类
else if ($act == "delete") {
    $cat_id = intval($_POST['company_new_cat_id']);
    // var_dump($id);
    $res['status'] = 0;
    //检测是不是有子项目
    $sql = "select * from ".$Base->table('company_news')." where c_id=".$id." and cat_type=".$cat_id;
    $resu = $Db->query($sql);
    if(empty($resu)){
        //验证是否是非法操作
        $Db->execSql("delete from " . $Base->table('company_new_cat') . " where id=$cat_id ");
        $res['status'] = 1;
    }else{
        $res['msg'] = "含有新闻，不能删除";
    }
    echo $Json->encode($res);
    exit;
}

$tp->assign('nav', '栏目管理');
$tp->assign('act', $act);

function productCatList($id,$page,$size)
{
    $spm = '';
    $sql = "SELECT new.*,admin.name,admin.icon FROM " . $GLOBALS['Base']->table('company_new_cat') ." as new " .
        'left join'.$GLOBALS['Base']->table('company_admin_new_cat').' as admin on admin.id=new.pid '.
        "where new.c_id=".$id." ";
    $rescount = count($GLOBALS['Db']->query($sql));
    $sql .= " ORDER BY sort asc, id asc LIMIT ".($page-1)*$size.", ".$size."";
    $list = $GLOBALS['Db']->query($sql);
    return array('list'=>$list,'count'=>$rescount,'spm'=>$spm);
}

?>